{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/guan/anaconda2/lib/python2.7/site-packages/sklearn/cross_validation.py:44: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "/home/guan/anaconda2/lib/python2.7/site-packages/sklearn/grid_search.py:43: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "#coding=utf-8\n",
    "%matplotlib inline\n",
    "import pandas as pd\n",
    "import xgboost as xgb\n",
    "from xgboost import XGBRegressor\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.metrics import precision_recall_fscore_support\n",
    "from sklearn.metrics import precision_score\n",
    "from sklearn.metrics import recall_score\n",
    "from sklearn.metrics import roc_auc_score\n",
    "from sklearn.metrics import confusion_matrix, mean_squared_error\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.font_manager as mfm\n",
    "import matplotlib as mpl\n",
    "font_path = \"/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc\"\n",
    "prop = mfm.FontProperties(fname=font_path)\n",
    "mpl.rcParams['font.sans-serif'] = ['wqy-zenhei']\n",
    "mpl.rcParams['font.family'] ='sans-serif'\n",
    "\n",
    "# load data and split feature and label\n",
    "df = pd.read_csv('../data/hk_ipo_feature_engineered', sep='\\t', index_col='code', encoding='utf-8')\n",
    "Y = df['firstday_performance']\n",
    "X = df.drop('firstday_performance', axis = 1)\n",
    "# split data into train and test sets\n",
    "seed = 7\n",
    "test_size = 0.33\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=test_size, random_state=seed)\n",
    "# fit model no training data\n",
    "eval_set = [(X_test, y_test)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0643324471123\n"
     ]
    }
   ],
   "source": [
    "# fit model no training data\n",
    "xgb_model = xgb.XGBRegressor().fit(X_train,y_train)\n",
    "predictions = xgb_model.predict(X_test)\n",
    "actuals = y_test\n",
    "print mean_squared_error(actuals, predictions)\n",
    "\n",
    "#fig, ax = plt.subplots(1,1,figsize=(10, 25))\n",
    "#xgb.plot_importance(xgb_model, ax = ax)\n",
    "#xgb.plot_tree(xgb_model, num_trees=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "importance = xgb_model.booster().get_score(importance_type='weight')\n",
    "tuples = [(k, importance[k]) for k in importance]\n",
    "outfile = open('feature_importance', 'w')\n",
    "for item in tuples:\n",
    "    print >> outfile, item[0].encode('utf-8') + '\\t' + str(item[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
